package io.imp.modules.tasks.unionqrc.service;

import io.imp.modules.sys.service.SysDictService;
import io.imp.modules.tasks.unionqrc.entity.ItcsUnionpayCancelJnl;
import io.imp.modules.tasks.unionqrc.entity.ItcsUnionpayTxnJnl;
import io.imp.modules.tasks.unionqrc.entity.ItcsUnionpayTxnJnlPart;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * 功能描述:
 * <撤销文件数据处理>
 *
 * @ClassName: WorkItcsUnionpayReverseJnl
 * @Date: 2019年05月17日 13:31:04
 *
 * @Copyright: 2019 Inc. All rights reserved. 注意: 本内容仅限于内部传阅,禁止外泄以及用于其他的商业目
 */
public class WorkItcsUnionpayCancelJnl implements Callable {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private ItcsUnionpayCancelJnlService itcsUnionpayCancelJnlService;

    private ItcsUnionpayTxnJnlPartService itcsUnionpayTxnJnlPartService;

    private SysDictService sysDictService;

    private ItcsUnionpayTxnJnlService itcsUnionpayTxnJnlService;

    private List<ItcsUnionpayCancelJnl> source;

    private CountDownLatch countDownLatch;
    public WorkItcsUnionpayCancelJnl(ItcsUnionpayCancelJnlService itcsUnionpayCancelJnlService,
                                     ItcsUnionpayTxnJnlPartService itcsUnionpayTxnJnlPartService,
                                     ItcsUnionpayTxnJnlService itcsUnionpayTxnJnlService,
                                     SysDictService sysDictService, List<ItcsUnionpayCancelJnl> source,
                                     CountDownLatch countDownLatch){
        this.itcsUnionpayCancelJnlService = itcsUnionpayCancelJnlService;
        this.itcsUnionpayTxnJnlPartService = itcsUnionpayTxnJnlPartService;
        this.itcsUnionpayTxnJnlService = itcsUnionpayTxnJnlService;
        this.sysDictService = sysDictService;
        this.source = source;
        this.countDownLatch = countDownLatch;
    }
    @Override
    public String call() throws Exception {
        logger.info("撤销流水数据处理...");
        List<ItcsUnionpayTxnJnlPart> itcsUnionpayTxnJnlParts = new LinkedList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
        Calendar calendar = Calendar.getInstance();

        StringBuilder result = new StringBuilder();
        if (source.size()!=0){

            for (ItcsUnionpayCancelJnl cancel:source) {

                //获取原始渠道订单实体
                String origqryid = cancel.getOrigqryid();
                ItcsUnionpayTxnJnl itcsUnionpayTxnJnl = itcsUnionpayTxnJnlService.
                        getItcsUnionpayTxnJnlWithOne(new String[] { "queryid" }, new Object[] {origqryid});
                String txnno = itcsUnionpayTxnJnl.getTxnno();
                String orderid = itcsUnionpayTxnJnl.getOrderid();

                String txntype = cancel.getTxntype();
                txntype = StringUtils.isBlank(txntype)?"31":txntype;
                String txnsubtype = cancel.getTxnsubtype();
                txnsubtype = StringUtils.isBlank(txnsubtype)?"00":txnsubtype;
                result.append(cancel.getMerid()).append("|").append(cancel.getTermId()).append("|")
                        .append("").append("|").append(cancel.getOrderid())
                        .append("|").append(cancel.getTxntime()).append("|")
                        .append("").append("|").append(cancel.getTxnamt())
                        .append("|").append(txntype).append("|")
                        .append(txnsubtype).append("|").append(cancel.getTraceno())
                        .append("|").append(cancel.getRespcode()).append("|").append(cancel.getRespmsg())
                        .append("|").append(cancel.getSettledate()).append("|")
                        .append(cancel.getSettleamt()).append("|").append(itcsUnionpayTxnJnl.getAccno())
                        .append("|").append(itcsUnionpayTxnJnl.getOrderid()).append("|").append("消费撤销").append("\r\n");
                itcsUnionpayCancelJnlService.update("1",cancel.getTxnno());
            }
        }else {
            result.append("");
        }
        this.countDownLatch.countDown();
        return result.toString().replace("null","");
    }
}
